package cn.com.xy.sms.sdk.db.entity;

import android.content.ContentValues;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.ContactsDBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.log.LogManager;

/* SDK-428 huangzhiqiang 20160530 start */
public class ContactsManager {
    /**
     * 创建联系人表的 SQL 语句
     */
    public static final String CREATE_CONTACTS_SQL = "CREATE TABLE IF NOT EXISTS " + ContactsColumns.TABLE_NAME + "("
            + ContactsColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ContactsColumns.PHONE + " TEXT, "
            + ContactsColumns.NAME + " TEXT, " + ContactsColumns.DATA + " TEXT, " + ContactsColumns.UPDATE_TIME + " TEXT)";

    /**
     * 根据查询条件查询联系数据
     * 
     * @param projection
     * @param selection
     * @param selectionArgs
     * @param sortOrder
     * @return
     */
    public static XyCursor query(String[] projection, String selection, String[] selectionArgs, String sortOrder) {

        try {
            return ContactsDBManager.query(ContactsColumns.TABLE_NAME, projection, selection, selectionArgs, null, null,
                    sortOrder, null);
        } catch (Exception ex) {
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "ContactsManager query: ", ex);
            /*SDK-550 hewengao 20160719 end*/
        }
        return null;
    }

    /**
     * 新增联系人数据
     * 
     * @param values
     * @return
     */
    public static long insert(ContentValues values) {
        if (values == null) {
            values = new ContentValues();
        }
        XyCursor cursor = null;
        try {
            String whereClause = ContactsColumns.PHONE + " = ? ";
            String[] whereArgs = new String[] { values.getAsString(ContactsColumns.PHONE) };
            // 更新时间
            values.put(ContactsColumns.UPDATE_TIME, String.valueOf(System.currentTimeMillis()));
            cursor = ContactsDBManager.query(ContactsColumns.TABLE_NAME, new String[] { ContactsColumns.NAME },
                    whereClause, whereArgs);
            if (cursor != null && cursor.getCount() > 0) { // phone 存在数据库，更新数据
                return ContactsDBManager.update(ContactsColumns.TABLE_NAME, values, whereClause, whereArgs);
            } else { // 该 phone 不存在数据库中，插入到数据库中
                return ContactsDBManager.insert(ContactsColumns.TABLE_NAME, values);
            }
        } catch (Throwable ex) {
            /*SDK-550 hewengao 20160719 start*/
            LogManager.e(Constant.TAG, "ContactsManager insert: " + ex.getMessage(), ex);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(cursor, true);
        }
        return -1;
    }

    /**
     * 删除联系人数据
     * 
     * @param selection
     * @param selectionArgs
     * @return
     */
    public static int delete(String selection, String[] selectionArgs) {
        try {
            return ContactsDBManager.delete(ContactsColumns.TABLE_NAME, selection, selectionArgs);
        } catch (Throwable ex) {
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "ContactsManager delete: ", ex);
            /*SDK-550 hewengao 20160719 end*/
        }
        return -1;
    }
}
/* SDK-428 huangzhiqiang 20160530 end */
